package com.nm.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nm.commom.Result;
import com.nm.mapper.StallMapper;
import com.nm.mapper.StopMapper;
import com.nm.pojo.Daom;
import com.nm.pojo.Stall;
import com.nm.pojo.Stop;
import com.nm.service.StallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.invoke.LambdaMetafactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static java.lang.System.out;
/**
 *车位接口
 */
//@CrossOrigin
@RestController
@RequestMapping(value = "/stall")
public class StallController {

    @Autowired
    private StallService stallService;
    @Autowired
    private StallMapper stallMapper;
    @Autowired
    private StopMapper stopMapper;


    /**
     *查询全部车位
     */
    //
    @GetMapping (value = "/queryAll")
    public Result queryAllStall(){
        List<Stall> list = stallMapper.selectList(null);
        return Result.success(list);
    }

    /**
     *根据ID查询信息
     */
    //
    @PostMapping(value = "/queryid")
    public Result queryId(@RequestBody Daom daom){
        int stallId = daom.getId();
        Stall stall = stallMapper.selectById(stallId);
        return Result.success(stall);
    }

    /**
     *增加车位
     */
    //
    @PostMapping(value = "/addstall")
    public Result addstall(@RequestBody Stall stall) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String stallTime = stall.getStallTime();
        double time = df.parse(String.valueOf(stallTime)).getTime();
        String endTime = stall.getEndTime();
        double time1 = df.parse(String.valueOf(endTime)).getTime();
        int i = stallMapper.insert(stall);
        return Result.success(i);
    }

    /**
     *修改车位信息
     */
    //
    @PostMapping(value = "/update")
    public Result updateStall(@RequestBody Stall stall){
        out.println(stall);
        stallMapper.updateById(stall);
        return Result.success();
    }

    /**
     *删除车位信息
     */
    //
    @PostMapping(value = "/delete")
    public  Result deleteStall(@RequestBody int[] arr){
        for(int id: arr){
        stallMapper.deleteById(id);
        }
        return Result.success();
    }

    /**
     *出租车位
     */
    //
    @PostMapping(value = "/add")
    public Result addStall(@RequestBody Stall stall)  {
        stall.setStallState("出租");
        int i = stallMapper.insert(stall);
        return Result.success(i);
    }

    /**
     *禁租车位
     */
    //
    @PostMapping(value = "/jinzu")
    public Result jianzuStall(@RequestParam int stallId) {
        QueryWrapper<Stall> stallQueryWrapper = new QueryWrapper<>();
        stallQueryWrapper.eq("stall_id",stallId);
        Stall stall = stallMapper.selectOne(stallQueryWrapper);
        stall.setStallState("禁租");
        stallMapper.updateById(stall);
        return Result.success();
    }

    /**
     *查询全部出租车位
     */
    //
    @GetMapping(value = "/queryFree")
    public Result queryStall(){
        QueryWrapper<Stall> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("stall_state","出租");
        List<Stall> stalls = stallMapper.selectList(objectQueryWrapper);
        return Result.success(stalls);
    }

    /**
     *查询某用户出租的车位
     */
    //
    @PostMapping(value = "/queryxcx")
    public Result queryXcx(@RequestParam String userName){
        QueryWrapper<Stall> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        List<Stall> list = stallMapper.selectList(wrapper);
        return Result.success(list);
    }

    /**
     *搜索车位
     */
    //
    @PostMapping(value = "/seek")
    public Result seekStall(@RequestBody Stall stall){
        List<Stall> list = stallService.seekStall(stall);
        return Result.success(list);
    }

    /**
     *查询车位评分
     */
    @PostMapping(value = "/selectScore")
    public Result selectScore(@RequestParam int stallId){
        QueryWrapper<Stop> stopQueryWrapper = new QueryWrapper<Stop>();
        stopQueryWrapper.eq("stall_id",stallId);
        List<Stop> stops = stopMapper.selectList(stopQueryWrapper);
        /*List<Map<Object, Object>> maps = new ArrayList<>();*/
        ArrayList<Integer> list = new ArrayList<>();
        for (Stop stop : stops) {
            /*HashMap<Object, Object> objectObjectHashMap = new HashMap<>();*/
            if (stop.getScore()!=0){
                list.add(stop.getScore());
            }
        }
        int sum = 0;
        for (Integer integer : list) {
            sum=sum+integer;
        }
        if (list.size()==0){
            return Result.success("尚无评分");
        }
        return Result.success(sum/list.size());
    }

    /**
     *查询某车位的历史租用信息
     */
    @GetMapping(value = "/select/stallInfo")
    public Result selectStallInfo(@RequestParam int stallId){
        QueryWrapper<Stop> stopQueryWrapper = new QueryWrapper<Stop>();
        stopQueryWrapper.eq("stall_id",stallId);
        List<Stop> stops = stopMapper.selectList(stopQueryWrapper);
        List<Map<Object, Object>> maps = new ArrayList<>();
        for (Stop stop : stops) {
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("nickName",stop.getNickName());
            objectObjectHashMap.put("startTime",stop.getStartTime());
            objectObjectHashMap.put("endTime",stop.getOverTime());
            objectObjectHashMap.put("stopPlace",stop.getStopPlace());
            objectObjectHashMap.put("userMoney",stop.getStopMoney());
            objectObjectHashMap.put("userStatus",stop.getUseStatus());
            QueryWrapper<Stall> stallQueryWrapper = new QueryWrapper<>();
            stallQueryWrapper.eq("user_name",stop.getNickName());
            try {
                List<Stall> stalls = stallMapper.selectList(stallQueryWrapper);
                Stall stall = stalls.get(0);
                objectObjectHashMap.put("phone",stall.getOwnerPhone());
            }catch (Exception e){
                objectObjectHashMap.put("phone",null);
            }
            maps.add(objectObjectHashMap);
        }
        return Result.success(maps);
    }

}
